#ifndef __MyTask_H__
#define __MyTask_H__

#include "TcpConnection.h"
#include "CandidateWord.h"
#include "WebpageSearch.h"
#include "log4cppuse.h"
#include "KeyWordLRUCache.h"


class MyTask
{
public:
    MyTask(const Message & message, const TcpConnectionPtr & con)
    : _readmessage(message)
    , _con(con)
    {}

    void process()
    {
        //如何处理msg

        // _msg = "msg :" + _msg;

        //这样做虽然能确保数据处理完毕之后再进行发送行为
        //但是是由子线程发送客户端，与设计不符
        //所以TcpConnection需要提供一个函数
        //让子线程将处理后的数据发给主线程
        /* _con->send(msg); */

        
        Message sendmessage;

        if(_readmessage.atg == 1)
        {
             sendmessage.atg=1;
             
             // 从缓存中获取值
             KeyWordLRUCache& cache = KeyWordLRUCache::getInstance();
             string cachevalue=cache.get(_readmessage.msg);
             if(cachevalue.empty())
             {
                 CandidateWordSet& candidateWordSet=CandidateWordSet::getCandidateWordSet();
                 sendmessage.msg=candidateWordSet.process(_readmessage.msg);
                 sendmessage.length=sendmessage.msg.size();
                 cache.put(_readmessage.msg,sendmessage.msg);
                
             }else{
                 sendmessage.msg=cachevalue;
                 sendmessage.length=sendmessage.msg.size();
             }
             
            
        }else if(_readmessage.atg == 2)
        {
            sendmessage.atg=2;
            WebpageSearch& webpageSearch=WebpageSearch::getWebpageSearch();
            sendmessage.msg=webpageSearch.search(_readmessage.msg);
            sendmessage.length=sendmessage.msg.size();

        }else{
    
            sendmessage.atg=0;
            sendmessage.msg="error tag";
            sendmessage.length=sendmessage.msg.size();
       
    }
        string log="send message tag: "+std::to_string(sendmessage.atg)+" send message length: "+std::to_string(sendmessage.length)+" send message: "+sendmessage.msg;
        LOG_DEBUG(log.c_str());
        _con->sendInLoop(sendmessage);
    }

private:
    Message _readmessage;

    TcpConnectionPtr _con;
};



#endif

